// pages/message/messagesDetail/messagesDetail.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:0,
    hasMoreData:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var typeid = options.typeid;
    var title = options.title;
    wx.setNavigationBarTitle({
      title: title
    })
    this.setData({
      typeid:typeid
    })
    this.getList();
  },

  getList:function (){
    var page = this.data.page;
    var typeid = this.data.typeid;
    var that = this;
    utils.requestUrl({
      url:'/niu/apiv3/noti_getlist.php',
      params: {
        markid:wx.getStorageSync('markId'),
        page:page,
        typeid:typeid
      }
    }).then((res) =>{
      console.log(res.data);
      var contentListTem = that.data.list;
        if (res.data.datacount == 0 || res.data.datacount == null) {
          that.setData({
            noContent:true,
            hasMoreData:true
          })
        }else{
          that.setData({
            noContent:false
          })
        }
        if (res.data.datacount > 0) {
          var contentList = res.data.data
          contentList.forEach((item) => {
            let str;
            if (item.info.indexOf('排骨') !== -1) {
                str = item.info.replace('排骨','蜜枣');
            }else if (item.info.indexOf('羊肉') !== -1) {
                str = item.info.replace('羊肉','猪肉');
            }else if (item.info.indexOf('豆沙馅') !== -1) {
              str = item.info.replace('豆沙馅','红豆');
          }else{
                str = item.info
            }
            //这里需要截取的内容
            item.subTime = utils.subTime(item.time)
            item.info = str
            item.huodanid = item.gotopage.substring(19)
            
          })
          if (contentList.length < res.data.pagesize) {
            that.setData({
              list:contentListTem.concat(contentList),
              hasMoreData: false
            })
          }else{
            that.setData({
              list: contentListTem.concat(contentList),
              hasMoreData: true,
              page: page + 1
          })
          }
        }
    })
  },
  //标记已读
  read:function () {
    var typeid = this.data.typeid;
    console.log(typeid);
    utils.requestUrl({
      url:'/niu/apiv3/noti_callback.php',
      params: {
        markid:wx.getStorageSync('markId'),
        typeid:typeid
      }
    }).then((res) =>{
      console.log(res.data);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.read();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:0,
      list:[],
    })
    this.getList()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.getList()
    }
  }
})